package logic

import (
	"context"
	"errors"
	"goods_srv/model/mysql"
	"gorm.io/gorm"

	"goods_srv/goods"
	"goods_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetGoodsDetailsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetGoodsDetailsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetGoodsDetailsLogic {
	return &GetGoodsDetailsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品详情

func (l *GetGoodsDetailsLogic) GetGoodsDetails(in *goods.GetGoodsDetailsReq) (*goods.GetGoodDetailsResp, error) {
	g := mysql.Goods{}
	err := g.Details(in.GoodsId)
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		logx.Error("查询失败", err)
		return nil, err
	}
	return &goods.GetGoodDetailsResp{
		GoodsTitle: g.GoodsTitle,
		GoodsPrice: g.GoodsPrice,
		GoodsStock: g.GoodsStock,
		GoodsCover: g.GoodsCover,
		GoodStatus: int64(g.GoodsStatus),
	}, nil
}
